package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.LastTransferredAddressAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.utils.TimeListUilts;

import java.util.List;
import java.util.TimerTask;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.LastTransferredAddressBean;

/**
 * Created by Jason Chen on 2017/2/22.
 * 转后转入地
 */

public class LastTransferredAddressActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener {
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;

    private LastTransferredAddressAdapter addressAdapter;
    private List<LastTransferredAddressBean> addressList;
    private String areaAbbreviationName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_last_transferred_address);
        ButterKnife.inject(this);
        getAddressList();

    }

    private void initViews() {
        addressAdapter = new LastTransferredAddressAdapter(this,R.layout.layout_item_last_transferred_address,addressList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(addressAdapter);
        addressAdapter.setOnItemClickListener(this);
    }

    @OnClick({R.id.iv_return, R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.tv_sure:
                Intent intent = new Intent();
                intent.putExtra("areaAbbreviationName",areaAbbreviationName);
                setResult(RequestAndResultCode.LastTransferredAddressActivity_ResultCode,intent);
                finish();
                break;
        }
    }

    public void getAddressList() {
        new Thread(){
            @Override
            public void run() {
                addressList= TimeListUilts.getLastAddressList(LastTransferredAddressActivity.this);
                runOnUiThread(initViewRunnable);
            }
        }.start();
    }
    private Runnable initViewRunnable = new TimerTask() {
        @Override
        public void run() {
            initViews();
        }
    };
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        for (int i = 0; i < addressList.size(); i++) {
            if(i == position){
                addressList.get(i).setCheck(true);
            }else{
                addressList.get(i).setCheck(false);
            }
        }
        areaAbbreviationName = addressList.get(position).getProvince();
        addressAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
